package com.ruoyi.common.datascope.service;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.constant.RuoyiSecurityConstants;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.dto.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

/**
 * 同步调用用户服务
 *
 * @author ruoyi
 */
@Service
public class AwaitUserService {
    private static final Logger log = LoggerFactory.getLogger(AwaitUserService.class);

//    @Autowired
//    private RemoteUserService remoteUserService;

/*    @DubboReference(interfaceClass = IUserApiService.class, check = false)
    private IUserApiService userApiService;*/


    /**
     * 查询当前用户信息
     *
     * @return 用户基本信息
     */
    public UserInfo info() {
        //系统用户
        if (SecurityUtils.getLoginUser().getUserId() == 0) {
            return RuoyiSecurityConstants.getSystemUserInfo();
//            return
        }
        String username = SecurityUtils.getUsername();
        //使用线程为了解决 获取用户信息方法中提前消费了翻页信息
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<UserInfo> future = executorService.submit(new Callable<UserInfo>() {
            @Override
            public UserInfo call() throws Exception {
//                UserInfo userInfo = userApiService.getUserInfo(username);
                UserInfo userInfo = null;
                return userInfo;
            }
        });
        // 获取当前的用户
        UserInfo userInfo = null;
        try {
            userInfo = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
//        UserInfo userInfo = userApiService.getUserInfo(username);
        if (StringUtils.isNull(userInfo)) {
            log.info("数据权限范围查询用户：{} 不存在.", username);
            return null;
        }
        return userInfo;
    }
}
